#!/usr/bin/python3
import os, sys, json

#python3 extract_origin_data.py

def extract_delete_dir(source_data_path, dir_rest, extract_dir = "GU"):
    print('#' * 50)
    print('Start Checking File...')

    rest_dir = os.path.join(source_data_path, dir_rest)
    rm_dir_list = []
    remain_list = []
    # 输出所有文件和文件夹
    for sub_dir in os.listdir(rest_dir):
        # 处理T1数据
        full_sub_dir = os.path.join(rest_dir, sub_dir)
        full_surf_dir = os.path.join(full_sub_dir, 'surf')

        if extract_dir in sub_dir:
            if not os.path.exists(full_surf_dir):
                print('[FAILED DIR]: %s' % (full_sub_dir))
                rm_dir_list.append(full_sub_dir)
            else:
                remain_list.append(sub_dir)

    print("# Total failed Files: %d" % (len(rm_dir_list)))
    print('Checking File Finished!')
    print('#' * 50)

    projects_path = '/mri_projects/ASD_Analysis/'
    asd_list, hc_list = get_recon_list(projects_path, extract_dir)

    asd_remain_list = [sub_item for sub_item in remain_list if sub_item in asd_list]
    hc_remain_list = [sub_item for sub_item in remain_list if sub_item in hc_list]

    write_list_record(asd_remain_list, 'variability_list_ASD_' + extract_dir + '.txt')
    write_list_record(hc_remain_list, 'variability_list_HC_' + extract_dir + '.txt')

    return rm_dir_list

def get_recon_list(projects_path, extract_dir):
    lists_path = os.path.join(projects_path, 'Lists')

    asd_sub_file = os.path.join(lists_path, 'recon_list_' + extract_dir + '1.txt')
    hc_sub_file = os.path.join(lists_path, 'recon_list_' + extract_dir + '2.txt')

    asd_list = load_list_file(asd_sub_file)
    hc_list = load_list_file(hc_sub_file)

    return asd_list, hc_list

def load_list_file(file_name):
    list_data = []

    fp = open(file_name, "r")
    for line in fp.readlines():
        list_data.append(line.strip())
    fp.close()

    return list_data

def write_list_record(dir_list, file_name):
    lists_file_path = '/mri_projects/ASD_Analysis/Lists'
    full_file_path = os.path.join(lists_file_path, file_name)

    if not os.path.exists(full_file_path):
        fp = open(full_file_path, "w")
        for sub_item in dir_list:
            fp.write(sub_item + '\n')
        fp.close()

    print("[CLEAN UP]: Writing record in `%s`" % (full_file_path))

def rm_processed_file(dir_list, extract_dir):
    unfinished_dir = '/mri_projects/ASD_Analysis/DataProcessed/unfinished_rest'
    if not os.path.exists(unfinished_dir):
        os.makedirs(unfinished_dir)

    print('#' * 50)
    print('Start Removing Subject...')

    clean_up_list = []

    for dir_name in dir_list:
        cmd_str = "mv " + dir_name + " " + unfinished_dir
        if os.path.exists(dir_name):
            subj_name = dir_name.split('/')[-1]
            clean_up_list.append(subj_name)

            print("[Remove rest Subject]: ==> %s" % (dir_name))
            print("[CMD]: %s" % (cmd_str))

            os.system(cmd_str)

    print('Remove Subject Finished!')
    print('#' * 50)

    write_list_record(clean_up_list, 'cleanup_list_' + extract_dir + '.txt')

# 目标文件地址
dir_t1 = 'T1'
dir_rest = 'rest'
cen_symbol = "OHSU"
target_data_path = '/mri_projects/ASD_Analysis/DataProcessed'

dir_list = extract_delete_dir(target_data_path, dir_rest, cen_symbol)
rm_processed_file(dir_list, cen_symbol)
